//package com.chen.springboot.service.Impl;
//import com.chen.springboot.entity.Menu;
//import com.chen.springboot.mapper.MenuMapper;
//import org.springframework.stereotype.Service;
//
//import java.util.List;
//
//@Service
//public class MenuService {
//    private final MenuMapper menuMapper;
//
//    public MenuService(MenuMapper menuMapper) {
//        this.menuMapper = menuMapper;
//    }
//    public List<Menu> getMenuTree() {
//        return menuMapper.selectRootMenus();
//    }
//}
package com.chen.springboot.service.Impl;

import com.chen.springboot.entity.Menu;
import com.chen.springboot.entity.User;
import com.chen.springboot.exception.ServiceException;
import com.chen.springboot.mapper.MenuMapper;
import com.chen.springboot.service.Impl.UserServiceImpl;
import com.chen.springboot.utils.TokenUtils;
import org.apache.ibatis.jdbc.Null;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MenuService {

    private final MenuMapper menuMapper;
    private final UserServiceImpl UserServiceImpl;

    public MenuService(MenuMapper menuMapper, UserServiceImpl UserServiceImpl) {
        this.menuMapper = menuMapper;
        this.UserServiceImpl = UserServiceImpl;
    }

    public List<Menu> getMenuTree() {
        // 获取当前登录用户的 userId
        String userId = TokenUtils.getCurrUser();
        if (userId == null) {
            throw new ServiceException("401", "未登录或登录信息已过期");
        }

        // 根据 userId 获取用户角色
        User currentUser = UserServiceImpl.getById(Integer.parseInt(userId));

        // 获取所有菜单
        List<Menu> menuTree = menuMapper.selectRootMenus();

        // 如果当前用户是普通管理员（role == 2），则过滤掉包含 '/auth' 路径的菜单
        if (currentUser.getRole() == 2) {
            menuTree = filterAuthMenu(menuTree);
        }
        if(currentUser.getRole()==3){
            return null;
        }

        return menuTree;
    }

    // 递归过滤掉菜单中包含 '/auth' 路径的菜单
    private List<Menu> filterAuthMenu(List<Menu> menus) {
        List<Menu> filteredMenus = new ArrayList<>();

        for (Menu menu : menus) {
            // 如果菜单的 path 包含 /auth，跳过该菜单
//            if (menu.getPath() != null && menu.getPath().contains("/auth")) {
//                continue;
//            }
            // 如果菜单的 path 包含 /auth 或 /system/menuMange，跳过该菜单
            if (menu.getPath() != null && (menu.getPath().contains("/auth") || menu.getPath().contains("/system/menuMange"))) {
                continue;
            }
            // 如果菜单有子菜单，递归过滤
            if (menu.getChildren() != null && !menu.getChildren().isEmpty()) {
                menu.setChildren(filterAuthMenu(menu.getChildren()));
            }

            // 如果没有包含 '/auth'，则保留菜单
            filteredMenus.add(menu);
        }

        return filteredMenus;
    }

    // 添加菜单
    public void addMenu(Menu menu) {
        int result = menuMapper.insertMenu(menu);
        if (result != 1) {
            throw new ServiceException("500", "添加菜单失败");
        }
    }

    public int updateMenu(Menu menu) {
        return menuMapper.updateMenu(menu);
    }

    public int deleteMenu(Integer id) {
        return menuMapper.deleteById(id);
    }
}


